package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取报表
     */
    @Override
    public OverviewVo getReport() {
        //1.从Rides中获取报表数据
       Object overviewVoData = redisTemplate.opsForValue().get("OverviewVoData");//获取数据
        //1.1判断数据是否为空
        if(overviewVoData!=null){
            log.info("从Redis中获取报表数据");
            return (OverviewVo) overviewVoData;
        }
        //2.获取线索记录
        OverviewVo clueOverviewVo=clueMapper.getClueOverview();
        //2.1获取商机记录
        OverviewVo businessOverviewVo=businessMapper.getBusinessOverview();
        //2.2把结果合并
        BeanUtils.copyProperties(clueOverviewVo,businessOverviewVo,"businessTotal","businessWaitAllot","businessWaitFollow","businessFollowing","businessFalse","businessConvertCustomer");
        //3.把结果保存到Redis中
        log.info("从数据库中获取报表数据");
        redisTemplate.opsForValue().set("OverviewVoData",businessOverviewVo,60, TimeUnit.SECONDS);
        return businessOverviewVo;
    }
}
